package com.zy.project1;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan
@PropertySource(value={"classpath:db.properties"})
public class AppConfig {
    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;
    @Value("${driverClassName}")
    private String driverClassName;
    @Value("${url}")
    private String url;



    //利用上面的属性值来创建druid数据源
    @Bean(initMethod = "init",destroyMethod="close")    //另外要注意: idea会对这个方法的返回值进行解析，判断是否有  initMethod指定方法  ,所以类型 要一致
    public DruidDataSource druidDataSource(){
        DruidDataSource ds=new DruidDataSource( );
        ds.setUsername(   username );
        ds.setPassword(   password );
        ds.setDriverClassName(driverClassName);
        ds.setUrl(   url );
        //数据源的更多参数配置
        ds.setInitialSize(  5 );
        ds.setMaxActive(  20 );
        ds.setMinIdle(  5 );
        return ds;
    }

}
